package com.study.springboot.jpa.service.impl;

import com.study.springboot.jpa.dao.TCityRepository;
import com.study.springboot.jpa.dao.THotelRepository;
import com.study.springboot.jpa.entity.THotel;
import com.study.springboot.jpa.service.THotelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class THotelServiceImpl implements THotelService {
    protected static final Logger logger = LoggerFactory.getLogger(THotelServiceImpl.class);
    @Autowired
    private TCityRepository tCityRepository;
    @Autowired
    private THotelRepository tHotelRepository;
    @Override
    public Page<THotel> queryTHotel(int currentPage,int pageSize,final String cityName,final String name,final Integer type,final String createTime) {
        Pageable pageable = PageRequest.of(--currentPage, pageSize);
        Page<THotel> list= tHotelRepository.findAll(where(cityName,name, type,createTime),pageable);
        return list;
    }

    private Specification<THotel> where(
            final String cityName,final String name,final Integer type,final String createTime){
        return new Specification<THotel>() {
            @Override
            public Predicate toPredicate(Root<THotel> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicates = new ArrayList<Predicate>();
                //机构
                if(cityName!=null&&!cityName.equals("")){
                    List<String> cityIds = tCityRepository.findIdsByName("%"+cityName+"%");
                    if(cityIds.size()>0&&cityIds.size()<1000)//如果太多就不管了这个条件了
                        predicates.add(root.<String>get("id").in(cityIds));//confirmOrgNo
                }
                //名字
                if(name!=null&&!name.equals("")){
                    predicates.add(cb.like(root.<String>get("name"),"%"+name+"%"));
                }
                //类型
                if(type!=null&&!type.equals(""))
                    predicates.add(cb.equal(root.<String>get("type"),type));
                //日期
                if(createTime!=null&&!createTime.equals("")){
                    //处理时间
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date startDate;
                    Date endDate;
                    try {
                        startDate = new java.sql.Date(format.parse(createTime).getTime());
                    } catch (ParseException e) {
                        startDate =  new java.sql.Date(946656000000L);//2000 01 01
                    }
                    Calendar calendar = Calendar.getInstance() ;
                    endDate = new java.sql.Date(calendar.getTime().getTime());
                    calendar = null;
                    logger.debug(format.format(endDate));
                    predicates.add(cb.between(root.<Date>get("createtime"),startDate,endDate));
                }
//                //审核人
//                if(checker!=null&&!checker.equals("")){
//                    List<String> userIds = userDao.findByName(checker);
//                    if(userIds.size()>0&&userIds.size()<1000)//如果太多就不管了这个条件了
//                        predicates.add(root.<String>get("confirmUserId").in(userIds));
//                }
                return query.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();
            }
        };
    }
}
